package cn.powerone.autotest.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.common.annotation.DataSource;
import cn.powerone.common.constant.UserConstants;
import cn.powerone.common.core.text.Convert;
import cn.powerone.common.enums.DataSourceType;
import cn.powerone.common.exception.ServiceException;
import cn.powerone.common.utils.StringUtils;
import cn.powerone.autotest.domain.Project;
import cn.powerone.autotest.mapper.ProjectMapper;
import cn.powerone.autotest.service.IProjectService;

/**
 * 项目管理 服务层实现
 * 
 * @author autotest
 */
@Service
public class ProjectServiceImpl implements IProjectService
{
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询项目信息
     * 
     * @param projectId 项目ID
     * @return 项目信息
     */
    @Override
    @DataSource(DataSourceType.MASTER)
    public Project selectProjectById(String projectId)
    {
        Project project = new Project();
        project.setProjectId(projectId);
        return projectMapper.selectProject(project);
    }

    /**
     * 查询项目列表
     * 
     * @param project 项目信息
     * @return 项目集合
     */
    @Override
    @DataSource(DataSourceType.MASTER)
    public List<Project> selectProjectList(Project project)
    {
        return projectMapper.selectProjectList(project);
    }

    /**
     * 新增项目
     * 
     * @param project 项目信息
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.MASTER)
    public int insertProject(Project project)
    {
        return projectMapper.insertProject(project);
    }

    /**
     * 修改项目
     * 
     * @param project 项目信息
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.MASTER)
    public int updateProject(Project project)
    {
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目信息
     * 
     * @param projectIds 需要删除的项目ID
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.MASTER)
    public int deleteProjectByIds(String[] projectIds)
    {
        return projectMapper.deleteProjectByIds(projectIds);
    }

    /**
     * 删除项目信息
     * 
     * @param projectId 需要删除的项目ID
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.MASTER)
    public int deleteProjectById(String projectId)
    {
        return projectMapper.deleteProjectById(projectId);
    }

    /**
     * 校验项目名称是否唯一
     * 
     * @param project 项目信息
     * @return 结果
     */
    @Override
    public boolean checkProjectNameUnique(Project project)
    {
        String projectId = StringUtils.isNull(project.getProjectId()) ? "" : project.getProjectId();
        Project info = projectMapper.checkProjectNameUnique(project.getProjectName());
        if (StringUtils.isNotNull(info) && !info.getProjectId().equals(projectId))
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}